﻿using Modbus.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Input;
using YmSoft.WPF.Core;

namespace YmSoft.WPF.Modbus
{
    public class MVModbus : BaseNotifyPropertyChanged
    {
        private static Thread runThread = null;

        public static SerialPort runSerialPort = null;

        internal Thread RunThread
        {
            get
            {
                return runThread;
            }
            set
            {
                runThread = value;
            }
        }

        internal SerialPort RunSerialPort
        {
            get
            {
                return runSerialPort;
            }
            set
            {
                runSerialPort = value;
            }
        }

        public MVModbus()
        {
            this.MVReadCoils = new MVReadCoils(this);
            this.MVReadHoldingRegisters = new MVReadHoldingRegisters(this);
            this.MVReadInputs = new MVReadInputs(this);
            this.MVWriteMultipleCoils = new MVWriteMultipleCoils(this);
            this.MVWriteMultipleRegisters = new MVWriteMultipleRegisters(this);
        }
        string prot = "COM3";
        public string Port
        {
            get
            {
                return this.prot;
            }

            set
            {
                if (value != this.prot)
                {
                    this.prot = value;
                    NotifyPropertyChanged("Port");
                }
            }
        }


        int baudRate = 9600;
        public int BaudRate
        {
            get
            {
                return this.baudRate;
            }

            set
            {
                if (value != this.baudRate)
                {
                    this.baudRate = value;
                    NotifyPropertyChanged("BaudRate");
                }
            }
        }

        public MVReadCoils MVReadCoils { get; private set; }

        public MVReadHoldingRegisters MVReadHoldingRegisters { get; private set; }

        public MVReadInputs MVReadInputs { get; private set; }

        public MVWriteMultipleCoils MVWriteMultipleCoils { get; private set; }

        public MVWriteMultipleRegisters MVWriteMultipleRegisters { get; private set; }

        string message = "开始";
        public string Message
        {
            get
            {
                return this.message;
            }

            set
            {
                if (value != this.message)
                {
                    this.message = value;
                    NotifyPropertyChanged("Message");
                }
            }
        }


        string log = "";
        public string Log
        {
            get
            {
                return this.log;
            }

            set
            {
                if (value != this.log)
                {
                    this.log = value;
                    NotifyPropertyChanged("Log");
                }
            }
        }


        ICommand stopCommand = null;
        public ICommand StopCommand
        {
            get
            {
                if (stopCommand == null)
                {
                    stopCommand = new RelayCommand(() =>
                    {
                        //PrivateSendMessage();
                        Stop();

                    });

                }
                return stopCommand;
            }
        }

        public void Stop()
        {
            if (RunSerialPort != null)
            {
                if (RunSerialPort.IsOpen)
                {
                    RunSerialPort.Close();
                }
                RunSerialPort.Dispose();
            }

            RunSerialPort = null;

            if (RunThread == null)
            {
                return;
            }

            RunThread.Abort();
            RunThread = null;
        }
    }
}
